package cn.edu.gzhmt.category.admin.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import cn.edu.gzhmt.category.admin.service.CategoryExcetpin;
import cn.edu.gzhmt.category.admin.service.IAdminCategoryService;
import cn.edu.gzhmt.category.admin.service.impl.AdminCategoryServiceImpl;
import cn.edu.gzhmt.category.domain.Category;
import cn.edu.gzhmt.utils.commons.CommonUtils;
import cn.edu.gzhmt.utils.web.servlet.BaseServlet;

/**
 * 后台管理分类模块：web层
 * @author azhuo
 *
 */
public class AdminCategoryServlet extends BaseServlet {

	private IAdminCategoryService adminCategoryService = new AdminCategoryServiceImpl();

	/**
	 * 加载所有分类
	 * 相应ajax请求，输出json格式数据
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String loadCategory(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		List<Category> categoryList = adminCategoryService.loadCategory();

		JSONArray json = JSONArray.fromObject(categoryList);
		response.getWriter().print(json.toString());

		return null;
	}

	/**
	 * 查询所有分类
	 * 把数据保存到request域中
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findAllCategory(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		List<Category> categoryList = adminCategoryService.loadCategory();

		request.setAttribute("categoryList", categoryList);

		return "f:/adminjsps/cart/cartList.jsp";
	}

	/**
	 * 根据id加载分类
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String loadCategoryById(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String cid = request.getParameter("cid");

		Category category = adminCategoryService.loadCategoryById(cid);

		request.setAttribute("category", category);

		return "f:/adminjsps/cart/cartEdit.jsp";
	}

	/**
	 * 根据分类id修改分类
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String editCategory(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// 封装数据
		Category formCategory = CommonUtils.toBean(request.getParameterMap(),
				Category.class);

		// 进行修改
		adminCategoryService.editCategory(formCategory);

		// 加载分类
		List<Category> categoryList = adminCategoryService.loadCategory();
		request.setAttribute("categoryList", categoryList);

		return "f:/adminjsps/cart/cartList.jsp";
	}

	/**
	 * 添加分类
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addCategory(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Category form = CommonUtils.toBean(request.getParameterMap(),
				Category.class);
		form.setCid(CommonUtils.uuid());

		try {
			adminCategoryService.addCategory(form);

		} catch (CategoryExcetpin e) {
			request.setAttribute("msg", e.getMessage());
		}

		return findAllCategory(request, response);
	}

	/**
	 * 删除分类
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String removeCategory(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String cid = request.getParameter("cid");

		adminCategoryService.removeCategory(cid);

		response.getWriter().print("true");

		return null;
	}

	/**
	 * 批量删除分类
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String removeAllCategory(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String cnames = request.getParameter("cnames");

		// 批量删除分类
		adminCategoryService.removeAllCategory(cnames);

		response.getWriter().print("true");

		return null;
	}

}
